﻿function $id(ele) {
    return document.getElementById(ele);
}

addLoadFuc(function() {
    if ($id("ipt_dep_sel")) EventUtil.addHandler($id("ipt_dep_sel"), "click", bindOrgs);
    if ($id("ipt_role_sel")) EventUtil.addHandler($id("ipt_role_sel"), "click", bindRole);
    if ($id("ipt_user_sel")) EventUtil.addHandler($id("ipt_user_sel"), "click", bindUser);
    $id("ipt_dep_sel").style.backgroundColor = " #dda0dd"; //#f5f5f5
    bindOrgs();
});

var whichPanel = "org";
var _orgList = "";

function bindOrgs() {
    whichPanel = "org";
    //绑定组织
    $id("ipt_dep_sel").style.backgroundColor = " #dda0dd"; //#f5f5f5
    $id("ipt_role_sel").style.backgroundColor = " #f5f5f5";
    $id("ipt_user_sel").style.backgroundColor = " #f5f5f5";
    $id("id_btn_and").enable = true;
    $id("id_btn_not").enable = true;
    if (_orgList != "") {
        $id("id_div_left").innerHTML = _orgList;
        return;
    }
    createList(ns.GetOrgs().value, "org");
}

var _roleLists = "";

function bindRole() {
    whichPanel = "role";
    //绑定角色
    $id("ipt_role_sel").style.backgroundColor = " #dda0dd"; //#f5f5f5
    $id("ipt_dep_sel").style.backgroundColor = " #f5f5f5";
    $id("ipt_user_sel").style.backgroundColor = " #f5f5f5";
    $id("id_btn_and").enable = true;
    $id("id_btn_not").enable = true;
    if (_roleLists != "") {
        $id("id_div_left").innerHTML = _roleLists;
        return;
    }
    createList(ns.GetRoles().value, "role");
}

var _userLists = "";

function bindUser() {
    whichPanel = "user";
    //绑定用户
    $id("ipt_user_sel").style.backgroundColor = " #dda0dd"; //#f5f5f5
    $id("ipt_role_sel").style.backgroundColor = " #f5f5f5";
    $id("ipt_dep_sel").style.backgroundColor = " #f5f5f5";
    $id("id_btn_and").enable = false;
    $id("id_btn_not").enable = false;

    if (_userLists != "") {
        $id("id_div_left").innerHTML = _userLists;
        return;
    }
    createList(ns.GetUsers().value, "user");
}

function createList(dt, iptType) {
    if (dt == null || dt.Rows.length < 1) return;
    var htmls = "<ul id='id_ul_left'>";
    var count = dt.Rows.length;
    for (var i = 0; i < count; i++) {
        htmls += "<li wfid='" + iptType + "_" + dt.Rows[i].TID + "' wftype='" + iptType + "'><a onclick=\"clickthis(this)\">" + dt.Rows[i].TNAME + "</a></li>";
    }
    htmls += "</ul>";
    $id("id_div_left").innerHTML = htmls;
    switch (iptType) {
    case "org":
        _orgList = htmls;
        break;
    case "role":
        _roleLists = htmls;
        break;
    case "user":
        _userLists = htmls;
        break;
    }
}

var cur_id;
var cur_type;
var cur_innerHTML;
var old_id;
var old_type;
var old_innerHTML;

function clickthis(ipt) {
    changCurOld();
    var par = ipt.parentNode;
    cur_id = par.getAttribute("wfid");
    cur_type = par.getAttribute("wftype");
    cur_innerHTML = ipt.innerHTML;
}

function changCurOld() {
    old_id = cur_id;
    old_innerHTML = cur_innerHTML;
    old_type = cur_type;
}

function addToRight() {
    var left = $id("id_ul_left").querySelector("li[wfid='" + cur_id + "']");
    if (left == null) return;
    var list = $id("id_ul_right");

    var temp = left.cloneNode();

    temp.setAttribute("wfexpress", whichPanel + "=" + left.getAttribute("wfid"));
    temp.innerHTML = left.innerHTML;
    //var li = document.createElement("li");
    //li.setAttribute("wftype", cur_type);
    //li.setAttribute("wfid", cur_id);

    //li.innerHTML = "<a onclick=\"clickthis(this)\">" + cur_innerHTML + "</a>";
    var v = list.querySelector("li[wfid='" + cur_id + "']");
    if (v == null) list.appendChild(temp);
    left.parentNode.removeChild(left);
    changCurOld();
    clean();
}

function addAllToRight() {
    var listr = $id("id_ul_right");
    var listl = $id("id_ul_left").querySelector("li");
    if (listl == null) return;

    var count = listl.length;
    for (var i = 0; i < count; i++) {
        //var li = document.createElement("li");
        //li.setAttribute("wftype", listl[i].getAttribute("wftype"));
        //li.setAttribute("wfid", listl[i].getAttribute("wfid"));
        //li.innerHTML = listl[i].innerHTML;

        var temp = listl[i].cloneNode();
        temp.setAttribute("wfexpress", whichPanel + "=" + temp.getAttribute("wfid"));
        var v = listr.querySelector("li[wfid='" + cur_id + "']");
        if (v != null) listr.appendChild(temp);
    }
    $id("id_ul_left").length = 0;
    changCurOld();
    clean();
}

function addToLeft() {
    var right = $id("id_ul_right").querySelector("li[wfid='" + cur_id + "'");
    right.parentNode.removeChild(right);
    switch (whichPanel) {
    case "org":
        bindOrgs();
        break;
    case "role":
        bindRole();
        break;
    case "user":
        bindUser();
        break;
    }
    changCurOld();
    clean();
}

function addAllToLeft() {
    $id("id_ul_right").length = 0;
    switch (whichPanel) {
    case "org":
        bindOrgs();
        break;
    case "role":
        bindRole();
        break;
    case "user":
        bindUser();
        break;
    }
    cleanAll();
}

function addToRightAnd() {
    var right = $id("id_ul_right").querySelector("li[wfid='" + old_id + "']");
    var left = $id("id_ul_left").querySelector("li[wfid='" + cur_id + "']");

    if (old_id == "" || right == null || left == null || cur_type == old_type) {
        alert("请先选择右侧起始项 !");
        return;
    }

    var express = "";
    switch (whichPanel) {
    case "org":
        express = "org=" + left.getAttribute("wfid");
        break;
    case "role":
        express = "role=" + left.getAttribute("wfid");
        break;
    case "user":
        express = "user=" + left.getAttribute("wfid");
        break;
    }
    right.setAttribute("wfexpress", right.getAttribute("wfexpress") + " and " + express);
    right.children[0].innerHTML = right.children[0].innerHTML + "[且]" + left.children[0].innerHTML;
    left.parentNode.removeChild(left);
    cleanAll();
}

function addToRightNot() {
    var right = $id("id_ul_right").querySelector("li[wfid='" + old_id + "']");
    var left = $id("id_ul_left").querySelector("li[wfid='" + cur_id + "']");
    if (old_id == "" || right == null || left == null || cur_type == old_type || cur_id == old_id) {
        alert("请先选择右侧起始项 !");
        return;
    }

    var express = "";
    switch (whichPanel) {
    case "org":
        express = "org!=" + left.getAttribute("wfid");
        break;
    case "role":
        express = "role!=" + left.getAttribute("wfid");
        break;
    case "user":
        express = "user!=" + left.getAttribute("wfid");
        break;
    }
    right.setAttribute("wfexpress", right.getAttribute("wfexpress") + " and " + express);
    right.children[0].innerHTML = right.children[0].innerHTML + "[除]" + left.children[0].innerHTML;
    left.parentNode.removeChild(left);
    cleanAll();
}

function clean() {
    cur_id = "";
    cur_type = "";
    cur_innerHTML = "";
}

function cleanAll() {
    clean();
    old_id = "";
    old_innerHTML = "";
    old_type = "";
}

function btnOK() {
    var pw = window.opener;

    var list = $id("id_ul_right").querySelectorAll("li");
    var count = (list == null ? 0 : list.length);
    if (count == 0) {
        window.close(); //关闭当前页面
        return;
    }
    var htmls = list[0].getAttribute("wfexpress");
    var names = list[0].children[0].innerHTML;
    for (var i = 1; i < count; i++) {
        htmls += ";" + list[i].getAttribute("wfexpress");
        //-------------在属性界面展示活动的授权用户
        names += ";" + list[i].children[0].innerHTML;
    }
    //------------------------------------------
    //先看看该活动对应的用户组存在不? id_idv_users_cols
    var curid = getParamValue("curid");
    var wfuId = "wfu_" + curid.replace("node", "");
    var wfu = pw.document.getElementById(wfuId);
    if (wfu == null) {
        wfu = pw.document.createElement("wfuser");
        wfu.id = wfuId;
        wfu.setAttribute("wfactid", curid);
        pw.document.getElementById("id_idv_users_cols").appendChild(wfu);
    }
    wfu.innerHTML = htmls + "{" + names;
    pw.document.getElementById("lb_user_names").innerHTML = names;

    window.close(); //关闭当前页面
}

function btnCancel() {
    window.close();
}